package com.axx.android.profile.auth.activity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.axx.android.R;
import com.axx.android.base.BaseActivity;
import com.axx.android.common.BaseModel;
import com.axx.android.common.Constants;
import com.axx.android.utils.GsonUtils;
import com.axx.android.utils.HttpUtils;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegActivity extends BaseActivity {

    @Bind(R.id.btn_back)
    Button mBtnBack;
    @Bind(R.id.et_realname)
    EditText mEtRealname;
    @Bind(R.id.et_id_number)
    EditText mEtIdNumber;
    @Bind(R.id.et_phone_number)
    EditText mEtPhoneNumber;
    @Bind(R.id.btn_send_code)
    Button mBtnSendCode;
    @Bind(R.id.et_validate_code)
    EditText mEtValidateCode;
    @Bind(R.id.et_password)
    EditText mEtPassword;
    @Bind(R.id.btn_login)
    Button mBtnLogin;

    @Override
    public int getLayoutId() {
        return R.layout.activity_reg;
    }

    @Override
    public void init(Bundle savedInstanceState) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_send_code, R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_send_code:
                sendCode();
                break;
            case R.id.btn_login:
                reg();
                break;
        }
    }

    private void reg() {
        if (TextUtils.isEmpty(mEtPhoneNumber.getText().toString())) {
            showToast("请输入电话号码");
            return;
        }
        if (TextUtils.isEmpty(mEtValidateCode.getText().toString())) {
            showToast("请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(mEtPassword.getText().toString())) {
            showToast("请输入密码");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("realName", mEtRealname.getText().toString());
        map.put("idCard", mEtIdNumber.getText().toString());
        map.put("phone", mEtPhoneNumber.getText().toString());
        map.put("psw", mEtPassword.getText().toString());
        map.put("code", mEtValidateCode.getText().toString());
        HttpUtils.post(Constants.Url.Profile.PROFILE_REG, map, new HttpUtils.SJCallBack() {
            @Override
            public void onSuccess(String result) {
                try {
                    BaseModel baseModel = GsonUtils.fromJson(result, new TypeToken<BaseModel>() {
                    });
                    if (baseModel.isSuccess()) {
                        showToast("注册成功，请重新登录");
                        finish();
                    } else {
                        showToast(baseModel.message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError() {

            }
        });

    }

    private void sendCode() {
        if (TextUtils.isEmpty(mEtPhoneNumber.getText().toString())) {
            showToast("请输入手机号");
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", mEtPhoneNumber.getText().toString());
        map.put("codeType", "1");
        HttpUtils.post(Constants.Url.Profile.PROFILE_SEND_AUTH_CODE, map, new HttpUtils.SJCallBack() {
            @Override
            public void onSuccess(String result) {
                try {
                    BaseModel model = GsonUtils.fromJson(result, new TypeToken<BaseModel>() {
                    });
                    if (model.isSuccess()) {
                        showToast("验证码发送成功");
                        mBtnSendCode.setEnabled(false);
                        handler.postDelayed(runnable, 1000);
                    } else {
                        showToast(model.message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    showErrorParse();
                }
            }

            @Override
            public void onError() {
                showErrorNet();
            }
        });
    }

    private Handler handler = new Handler();
    private int time = 60;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            time--;
            mBtnSendCode.setText("剩 余" + time);
            if (time == 0) {
                time = 60;
                mBtnSendCode.setText("发送验证码");
                mBtnSendCode.setEnabled(true);
                handler.removeCallbacks(runnable);
            } else {
                handler.postDelayed(this, 1000);
            }
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        handler.removeCallbacks(runnable);
    }
}
